package com.mms.web.controller;

import com.mms.model.AjaxResult;
import com.mms.model.pojo.SysMenu;
import com.mms.model.pojo.SysUser;
import com.mms.service.SysMenuService;
import com.mms.service.SysUserService;
import com.mms.utils.SysConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @auth wzy
 * @date 2023/11/23 9:26
 */

@Controller
public class LoginController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;

    @PostMapping("/login")
    @ResponseBody
    public AjaxResult login(HttpSession session, String username, String password){

        // result:code,msg,data
        SysUser sysUser = sysUserService.login(username, password);

        if (sysUser == null){
            return AjaxResult.error("登录失败");
        }

        // 将用户存到session中
        session.setAttribute(SysConst.SYS_USER_SESSION,sysUser);

        return AjaxResult.success();
    }


    @GetMapping("/sys/index")
    public String index(HttpServletRequest request){
        // 获取菜单
        List<SysMenu> menuTreeList = sysMenuService.treeList();

        request.setAttribute("menuTreeList", menuTreeList);

        return "index";
    }

    @GetMapping("/sys/welcome")
    public String welcome(){

        return "welcome";
    }
}
